package com.chezai.client.dao.manager;

import android.text.TextUtils;

import com.chezai.client.bean.TerminalBaseInfo;
import com.chezai.client.dao.DaoManager;
import com.chezai.client.dao.TerminalBaseInfoDao;
import com.chezai.client.global.Const;
import com.chezai.client.utils.AESUtil;
import com.chezai.client.utils.PreferenceManager;
import com.chezai.client.utils.Util;
import com.jx.driving.response.Terminal0x8100Response;
import com.jx.driving.response.Terminal0x8701Response;

/**
 * 计时终端设备信息数据操作管理类
 * Created by likunyang on 2017/8/24.
 */
public class TerminalBaseInfoDaoManager {

    private static TerminalBaseInfoDaoManager newInstance;
    private String mDeviceNo;
    private TerminalBaseInfoDao dao;
    private TerminalBaseInfo mBaseInfo;

    private TerminalBaseInfoDaoManager() {
        dao = DaoManager.getInstance().getDaoSession().getTerminalBaseInfoDao();
    }

    public static TerminalBaseInfoDaoManager getInstance() {
        if (newInstance == null) {
            newInstance = new TerminalBaseInfoDaoManager();
        }
        return newInstance;
    }


    /**
     * 插入或更新 终端基础信息
     *
     * @param object 只支持 Terminal0x8701Response,Terminal0x8100Response 两协议的数据保存
     */
    public void insertAndUpdateTerminalBaseInfo(Object object) {
        TerminalBaseInfo terminalBaseInfo = getTerminalBaseInfo();
        boolean isInsert = terminalBaseInfo == null;//是否插入新数据，false 不插入新数据即更新数据，true插入新数据
        if (isInsert) {
            terminalBaseInfo = new TerminalBaseInfo();
        }
        if (object instanceof Terminal0x8701Response) {
            Terminal0x8701Response ox8701 = (Terminal0x8701Response) object;
            terminalBaseInfo.setCityId(ox8701.getCityId());
            terminalBaseInfo.setProvinceId(ox8701.getProvinceId());
            terminalBaseInfo.setLoginType(ox8701.getLoginType());
            terminalBaseInfo.setIdentification(ox8701.getIdentification());
            terminalBaseInfo.setSchoolName(ox8701.getSchoolName());
            terminalBaseInfo.setSim(ox8701.getSim());
            terminalBaseInfo.setType(ox8701.getType());
            terminalBaseInfo.setSignColor(ox8701.getSignColor());
            terminalBaseInfo.setDeviceBelong(ox8701.getDeviceBelong());
            terminalBaseInfo.setInscode(ox8701.getInscode());
        } else if (object instanceof Terminal0x8100Response) {
            Terminal0x8100Response ox8100 = (Terminal0x8100Response) object;
            terminalBaseInfo.setDevice(ox8100.getDevice());
            terminalBaseInfo.setCert(ox8100.getCert());
            terminalBaseInfo.setPlatform(ox8100.getPlatform());
            terminalBaseInfo.setTraningOrg(ox8100.getTraningOrg());
            terminalBaseInfo.setCertPassword(AESUtil.getEncryptCertPassword(ox8100.getCertPassword()));
        } else {
            return;
        }
        if (isInsert) {
            long terminalBaseInfoId = dao.insert(terminalBaseInfo);
            PreferenceManager.putLong(Const.SP_TERMINAL_BASE_INFO_ID, terminalBaseInfoId);
        } else {
            dao.update(terminalBaseInfo);
        }
    }


    /**
     * 获取计时终端类型
     *
     * @return
     */
    public int getTerminalType() {
        if (getTerminalBaseInfo() != null) {
            return getTerminalBaseInfo().getType();
        }
        return 0;
    }

    /**
     * 获取计时终端编号
     *
     * @return
     */
    public String getTerminalDevice() {
        if (TextUtils.isEmpty(mDeviceNo)) {
            if (getTerminalBaseInfo() != null) {
                mDeviceNo = getTerminalBaseInfo().getDevice();
                return mDeviceNo;
            }
        }
        return mDeviceNo;
    }

    /**
     * 获取终端电话号码
     *
     * @return
     */
    public String getSim() {
        if (getTerminalBaseInfo() != null) {
            return getTerminalBaseInfo().getSim();
        }
        return "";
    }

    /**
     * 获取终端基础信息
     *
     * @return
     */
    public TerminalBaseInfo getTerminalBaseInfo() {
        long terminalBaseInfoId = PreferenceManager.getLong(Const.SP_TERMINAL_BASE_INFO_ID, 0);
        if (terminalBaseInfoId != 0) {
            return dao.loadByRowId(terminalBaseInfoId);
        }
        return null;
    }

    /**
     * 返回可登录方式
     *
     * @return
     */
    public byte[] getLoginType() {
        int loginType = getTerminalBaseInfo().getLoginType();
        byte[] bytes = Util.intToByte7(loginType);
        return bytes;
    }

    /**
     * 删除所有数据
     */
    public void remove() {
        dao.deleteAll();
    }

    public void update(TerminalBaseInfo baseInfo) {
        dao.update(baseInfo);
    }
}
